﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Models;
using BLL;
namespace AGVControlSystemPro
{
    public partial class FrmNodesEdit : Form
    {
        private LandNodeManager objLandNodeManager = new LandNodeManager();
        LandNode objLandNode = new LandNode();
        public GetInputLanNum modifyLandNum;// //根据委托创建委托对象
        public FrmNodesEdit()
        {
            InitializeComponent();
        }
        public FrmNodesEdit(string landId):this()
        {
            objLandNode = objLandNodeManager.GetLandNodeByLandId(landId);
            this.txtID.Text = objLandNode.ID.ToString();
            this.txtLandId.Text = objLandNode.LandId.ToString();
            this.txtName.Text = objLandNode.NodeName;
            this.txtXpos.Text = objLandNode.X.ToString();
            this.txtYpos.Text = objLandNode.Y.ToString();
            this.txtLandNum.Text = objLandNode.LandNum.ToString();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            #region 数据验证
            if (this.txtXpos.Text.Trim().Length == 0 || !Common.DataValidate.IsInteger(this.txtXpos.Text.Trim()))
            {
                this.txtXpos.Focus();
                if (CommonMethods.LanguageId == 3) MessageBox.Show("X軸座標の入力が正しくない！", "情報のヒント");
                else MessageBox.Show("X轴坐标输入不正确！", "信息提示");
                return;
            }
            if (this.txtYpos.Text.Trim().Length == 0 || !Common.DataValidate.IsInteger(this.txtYpos.Text.Trim()))
            {
                this.txtYpos.Focus();
                if (CommonMethods.LanguageId == 3) MessageBox.Show("Y軸座標の入力が正しくない！", "情報のヒント");
                else MessageBox.Show("Y轴坐标输入不正确！", "信息提示");
                return;
            }
            if (this.txtLandNum.Text.Trim().Length == 0 || !Common.DataValidate.IsPositiveInteger(this.txtLandNum.Text.Trim()))
            {
                this.txtLandNum.Focus();
                if (CommonMethods.LanguageId == 3) MessageBox.Show("地上番号の入力が正しくない！", "情報のヒント");
                else MessageBox.Show("地标号输入不正确！", "信息提示");
                return;
            }
            #endregion

            //数据封装
            LandNode landNode = new LandNode()
            {
                LandId = objLandNode.LandId,
                NodeName = "lbl"+ this.txtLandNum.Text.Trim(),
                NodeTypeId = objLandNode.NodeTypeId,
                ID = objLandNode.ID,
                X = Convert.ToInt32(this.txtXpos.Text.Trim()),
                Y = Convert.ToInt32(this.txtYpos.Text.Trim()),
                LandNum = Convert.ToInt32(this.txtLandNum.Text.Trim())
            };
            //后台数据访问
            try
            {
                //【1】执行修改的后台访问函数
                int result = objLandNodeManager.UpdateNodes(landNode);
                if (result > 0)
                {
                    //【2】执行数据的回传
                    modifyLandNum(landNode);
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    if (CommonMethods.LanguageId == 3) MessageBox.Show(this.txtLandNum.Text.Trim() + "地印がすでに存在している！", "情報のヒント");
                   else MessageBox.Show(this.txtLandNum.Text.Trim() + "号地标已存在，不能重复添加！", "修改提示");
                }
            }
            catch (Exception ex)
            {
                
                MessageBox.Show("地标数据修改失败！详细的错误信息："+ex.Message,"信息提示");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
